package com.frank.synchronizeProblem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

/**
 * @author 小石潭记
 * @date 2021/8/25 8:56
 * @Description: ${todo}
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private SynchronizedService synchronizedService;

    @RequestMapping("/add")
    public void addEmployee() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 1000; i++) {
            executorService.submit(() -> employeeService.addEmployee());
//            new Thread(() -> employeeService.addEmployee()).start();
        }
    }

    @RequestMapping("/test")
    public void test() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 1000; i++) {
            executorService.submit(() -> synchronizedService.synchronizedAddEmployee());
        }
    }

}

